package com.base.essearch.controller;

import com.base.essearch.video.model.Menu;
import com.base.essearch.video.model.Video;
import com.base.essearch.video.entity.Menus;
import com.base.essearch.video.entity.VideoMediaFile;
import com.base.essearch.video.service.IMenusService;
import com.base.essearch.video.service.IVideoMediaFileService;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @author lr
 * @version 0.1
 * @date 2020/12/01 15:36
 */
@Slf4j
@Controller
public class HtmlIndexController {

    @Autowired
    HttpServletRequest request;
    @Autowired
    IVideoMediaFileService videoMediaFileService;
    @Autowired
    private IMenusService iMenusService;

    /**
     * 普通搜索首页
     *
     * @return
     */
    @RequestMapping("search")
    public ModelAndView index() {
        ModelAndView mav = new ModelAndView("search/search");
        mav.addObject("type", "simple");
        return mav;
    }

    /**
     * 高亮显示搜索
     *
     * @return
     */
    @RequestMapping("/highlight")
    public ModelAndView docIndex() {
        ModelAndView mav = new ModelAndView("search/search");
        mav.addObject("type", "highlight");
        return mav;
    }


    /**
     * list-videos
     *
     * @return
     */
    @RequestMapping(value = {"list-videos.html", "", "index.html"})
    public ModelAndView video(String search, String selectType) {
        ModelAndView mav = new ModelAndView("search/list-videos");
        PageInfo<Video> videos = new PageInfo<>();
        List<Video> ss = new ArrayList<>();
        videos.setList(ss);
        List<Menu> menus = create();
        StringBuilder ti = new StringBuilder();
        for (Menu menu : menus) {
            ti.append(menu.getMenu()).append(",");
        }
        if (search != null) {
            ti.append(search).append(",");
        }

        request.setAttribute("titles", ti);
        request.setAttribute("videos", videos);
        request.setAttribute("menus", menus);
        request.setAttribute("search", search);
        request.setAttribute("selectType", selectType);
        return mav;
    }

    private List<Menu> create() {
        List<Menus> list = iMenusService.list();
        List<Menu> menus = new ArrayList<>();
        for (Menus m : list) {
            Menu menu = new Menu();
            menu.setMenuId(m.getName());
            menu.setMenu(m.getName());
            menus.add(menu);
        }
        return menus;
    }

    /**
     * submit-videos
     *
     * @return
     */
    @RequestMapping("submit-video.html")
    public ModelAndView submitVideo() {
        ModelAndView mav = new ModelAndView("search/submit-video");
        return mav;
    }


    @RequestMapping("upload.html")
    public ModelAndView upload() {
        ModelAndView mav = new ModelAndView("search/upload");
        return mav;
    }

    @RequestMapping("single-blog.html")
    public ModelAndView singleBlog() {
        ModelAndView mav = new ModelAndView("search/single-blog");
        return mav;
    }

    @RequestMapping("list-videos-search.html")
    public ModelAndView listvideossearch(String search, String selectType) {
        ModelAndView mav = new ModelAndView("search/list-videos-search");
        mav.addObject("type", "highlight");
        request.setAttribute("search", search);
        request.setAttribute("selectType", selectType);
        return mav;
    }

    @RequestMapping({"single-video.html"})
    public ModelAndView singlevideo(String file) {

        ModelAndView mav = new ModelAndView("search/single-video");
        VideoMediaFile byId = videoMediaFileService.getFileById(file);
        assertPathExist(byId);
        request.setAttribute("file", byId);
        VideoMediaFile pre = videoMediaFileService.getpre(byId.getFileRecord().getId());
        VideoMediaFile next = videoMediaFileService.getnext(byId.getFileRecord().getId());
        if (pre == null) {
            pre = videoMediaFileService.getnextlast(byId.getFileRecord().getId());
        }
        if (next == null) {
            next = videoMediaFileService.getprelast(byId.getFileRecord().getId());
        }
        request.setAttribute("pre", pre);
        request.setAttribute("next", next);
        return mav;
    }

    private void assertPathExist(VideoMediaFile byId) {
        if (!StringUtils.isEmpty(byId.getNetUrl())) {
            byId.setFileUrl(byId.getNetUrl());
        }
        // Assert.isTrue(new File(FileMvcConfig.getFilePath(byId.getFileUrl())).exists(), "视频溜到火星了");
    }

    @RequestMapping({"video_play.html"})
    public ModelAndView video_play(String file) {
        ModelAndView mav = new ModelAndView("search/video_play");
        VideoMediaFile byId = videoMediaFileService.getById(file);
        assertPathExist(byId);
        request.setAttribute("file", byId);
        return mav;
    }
}
